package com.tarena.lbs.business.dao.repository.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.business.dao.mapper.BusinessMapper;
import com.tarena.lbs.business.dao.repository.BusinessRepository;
import com.tarena.lbs.business.pojo.po.BusinessPO;
import com.tarena.lbs.business.pojo.query.BusinessQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public class BusinessRepositoryImpl implements BusinessRepository {

    @Autowired
    private BusinessMapper businessMapper;

    @Override
    public PageInfo<BusinessPO> pagesList(BusinessQuery query) {
        QueryWrapper<BusinessPO> queryWrapper = assembleBusinessQuery(query);
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<BusinessPO> pos = businessMapper.selectList(queryWrapper);
        return new PageInfo<>(pos);
    }

    @Override
    public BusinessPO getById(Integer businessId) {
        return businessMapper.selectById(businessId);
    }

    @Override
    public void save(BusinessPO po) {
        businessMapper.insert(po);
    }

    @Override
    public void updateById(BusinessPO po) {
        businessMapper.updateById(po);
    }

    private QueryWrapper<BusinessPO> assembleBusinessQuery(BusinessQuery query) {
        QueryWrapper<BusinessPO> queryWrapper = new QueryWrapper<>();
        //1.businessName 商家名称 模糊查询 where business_name like '%#{businessName}%'
        if (StringUtils.isNotBlank(query.getBusinessName())){
            queryWrapper.like("business_name",query.getBusinessName());
        }
        //2.business_head_phone 负责人电话 精确查询 where business_head_phone = businessHeadPhone
        if (StringUtils.isNotBlank(query.getBusinessHeadPhone())){
            queryWrapper.eq("business_head_phone",query.getBusinessHeadPhone());
        }
        //3.businessStatus 商家状态 模糊查询 where business_status = #{businessStatus}
        if (query.getBusinessStatus() != null){
            queryWrapper.eq("business_status",query.getBusinessStatus());
        }
        //4.startingTime 开始时间 模糊查询 where entry_time >= #{startingTime}
        if (query.getStartingTime() != null){
            queryWrapper.ge("entry_time",query.getStartingTime());
        }
        //5.endTime 结束时间 模糊查询 where entry_time <= #{endTime}
        if (query.getEndTime() != null){
            queryWrapper.le("entry_time",query.getEndTime());
        }
        return queryWrapper;

    }
}
